/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * This file is part of retabel.
 *
 * Copyright (c) 2011 by individual contributors as indicated by the
 * @author tags. See the copyright.txt file in the distribution
 * for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.retabel.util;

import org.slf4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * Takes log lines, writes all of them into the given Logger and stores the lines into his own cache, too
 * All log lines can be retrieved by calling getLogLines, then
 * <p/>
 * <h3>Extra-Info</h3>
 * Created: 20:36 20.08.11
 *
 * @author Herbert
 * @version 1.0
 */
public class LogLines {

    // constants

    // inner classes

    public class LogLine {
        private final String message;
        private final Throwable throwable;

        LogLine(String message, Throwable throwable) {
            this.message = message;
            this.throwable = throwable;
        }

        public String getMessage() {
            return message;
        }

        public Throwable getThrowable() {
            return throwable;
        }
    }

    // fields

    private final Logger log;
    private List<LogLine> logLines = new ArrayList<LogLine>();

    // constructor

    public LogLines(Logger log) {
        this.log = log;
    }

    // public methods

    public void log(String message) {
        log(message, null);
    }

    public void log(String message, Throwable throwable) {
        if (throwable != null) {
            log.info(message, throwable);
        } else {
            log.info(message);
        }

        logLines.add(new LogLine(message, throwable));
    }

    // beans

    public List<LogLine> getLogLines() {
        return Collections.unmodifiableList(logLines);
    }

}
